package org.example.memora.tools;

import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.PostCreateDTO;
import org.example.memora.common.utils.ChatContext;
import org.example.memora.service.PostService;
import org.hibernate.validator.internal.util.logging.Log_$logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.example.memora.common.result.Result;
import reactor.core.publisher.Mono;

import java.util.List;

import java.util.Collections;

@Slf4j
@Component
public class PostFunctionTool {

    @Autowired
    private PostService postService;


    @Tool(name = "create_post", value= "根据标题和内容创建一个校园生活分享帖子,创建完之后给用户回复发帖成功的提示语，如果发帖失败，则给出失败的原因")

    public String createPost(
            @P("title") String title,
            @P("content") String content,
            @P("userId")String userId
    ) {
        try {
            PostCreateDTO dto = new PostCreateDTO();
            dto.setTitle(title);
            dto.setContent(content);
            // 默认标签
            dto.setCategoryId(1);
            // 默认分类
            dto.setTags(List.of(1));
            dto.setImages(Collections.emptyList());
            //String userId= ChatContext.getCurrentUserId();
            log.info("智能助手获取的userID:"+userId);
            String res=postService.createPostByAI(dto,  userId);
            if (title == null || content == null) {
                return "很抱歉，我无法理解你的发帖内容，请再试一次。";
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return "发布失败 ❌：" + e.getMessage();
            // ✅ 避免返回 null
        }
    }
}

